package main

import (
	"io"
	"log"
	"net/http"
	_ "net/http/pprof"
	"os"
)

func main() {
	mux := http.NewServeMux()
	mux.HandleFunc("/healthz", healthz)
	mux.HandleFunc("/", rootHandler)
	log.Println("Starting Http Server...")
	err := http.ListenAndServe(":80", mux)
	if err != nil {
		log.Fatal(err)
	}
}

// healthz implementation task 4
func healthz(w http.ResponseWriter, r *http.Request) {
	w.WriteHeader(200)
	io.WriteString(w, "OK\n")
	log.Printf("access log: client ip: %s response code %d", r.RemoteAddr, 200)
}

func rootHandler(w http.ResponseWriter, r *http.Request) {
	// task 2
	version := os.Getenv("VERSION")
	w.Header().Add("version", version)

	// task 1
	for k, vl := range r.Header {
		// support multi header
		for _, v := range vl {
			w.Header().Add(k, v)
		}
	}
	// task 3, client ip is there, but how to get status code before it actual response ?
	log.Printf("access log: client ip: %s response code %d", r.RemoteAddr, 200)
}
